let Utils = Utils || {}


 // 获取文件后缀名
 Utils.getExt = (filename)=>{
	 if (typeof filename == 'string') {
	         return filename
	             .split('.')
	             .pop()
	             .toLowerCase()
	     } else {
	         throw new Error('filename must be a string type')
	     }
 }
 
 // 复制内容到剪贴板
 Utils.copyToBoard = (value)=>{
	 const element = document.createElement('textarea')
	     document.body.appendChild(element)
	     element.value = value
	     element.select()
	     if (document.execCommand('copy')) {
	         document.execCommand('copy')
	         document.body.removeChild(element)
	         return true
	     }
	     document.body.removeChild(element)
	     return false
 }
 
 // 休眠多少毫秒
 Utils.sleep = (ms)=>{
 	 return new Promise(resolve => setTimeout(resolve, ms))
 }
 
 // 生成随机字符串
 Utils.uuid = (length, chars)=>{
 	 chars = chars || '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
 	     length = length || 8
 	     let result = ''
 	     for (let i = length; i > 0; --i)
 	         result += chars[Math.floor(Math.random() * chars.length)]
 	     return result
 }
 
 // 简单的深拷贝
 Utils.deepCopy = (obj)=>{
	  if (typeof obj != 'object') {
	         return obj
	     }
	     if (obj == null) {
	         return obj
	     }
	     return JSON.parse(JSON.stringify(obj))
 }
 
 
 // 数组去重
 Utils.uniqueArray = (arr)=>{
	 if (!Array.isArray(arr)) {
	         throw new Error('The first parameter must be an array')
	     }
	     if (arr.length == 1) {
	         return arr
	     }
	     return [...new Set(arr)]
 }
 
 
 // 对象转化为FormData对象
 Utils.getFormData = (object)=>{
	  const formData = new FormData()
	     Object.keys(object).forEach(key => {
	         const value = object[key]
	         if (Array.isArray(value)) {
	             value.forEach((subValue, i) =>
	                 formData.append(key + `[${i}]`, subValue)
	             )
	         } else {
	             formData.append(key, object[key])
	         }
	     })
	     return formData
 }
 
 // 保留到小数点以后n位
 Utils.cutNumber = (number, no = 2)=>{
	 if (typeof number != 'number') {
	         number = Number(number)
	     }
	     return Number(number.toFixed(no))
 }
 
 //手机号校验
 Utils.isPhone = (value) => {
   let patten = /^1[3|4|5|7|8|9]\d{9}$/.test(value)
   return patten
 }
 
 // 邮箱校验
  Utils.isEmail = (value) =>{
 	 let reg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/.test(value)
 	 return reg
  }
  
 //姓名脱敏
 Utils.replaceName = (val) => {
   if (val.length == 2) {
     return val.replace(val.slice(-1), '*')
   } else if (val.length == 3) {
     return val.replace(val.slice(-2), '**')
   } else if (val.length == 4) {
     return val.replace(val.slice(-2), '**')
   }
 }
 
 //身份证脱敏
 Utils.replaceIdCard = (val) => {
   if (val.length == 15) {
     return val.replace(val.slice(4, 11), '*******')
   } else if (val.length == 18) {
     return val.replace(val.slice(4, 14), '**********')
   }
 }
 
 // 根据身份证号获取年龄
 Utils.GetAge = (identityCard) => {
   let len = (identityCard + "").length;
   if (len == 0) {
       return 0;
   } else {
       if ((len != 15) && (len != 18))//身份证号码只能为15位或18位其它不合法
       {
           return 0;
       }
   }
   let strBirthday = "";
   if (len == 18)//处理18位的身份证号码从号码中得到生日和性别代码
   {
       strBirthday = identityCard.substr(6, 4) + "/" + identityCard.substr(10, 2) + "/" + identityCard.substr(12, 2);
   }
   if (len == 15) {
       strBirthday = "19" + identityCard.substr(6, 2) + "/" + identityCard.substr(8, 2) + "/" + identityCard.substr(10, 2);
   }
   //时间字符串里，必须是“/”
   let birthDate = new Date(strBirthday);
   let nowDateTime = new Date();
   let age = nowDateTime.getFullYear() - birthDate.getFullYear();
   //再考虑月、天的因素;.getMonth()获取的是从0开始的，这里进行比较，不需要加1
   if (nowDateTime.getMonth() < birthDate.getMonth() || (nowDateTime.getMonth() == birthDate.getMonth() && nowDateTime.getDate() < birthDate.getDate())) {
       age--;
   }
   return age;
 }
 
 
 // 百度坐标系转换为gcj02 微信小程序api可用坐标
 Utils.bd09togcj02 = (bd_lon, bd_lat) => {
   let x_pi = 3.14159265358979324 * 3000.0 / 180.0;
   let x = bd_lon - 0.0065;
   let y = bd_lat - 0.006;
   let z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
   let theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
   let gg_lng = z * Math.cos(theta);
   let gg_lat = z * Math.sin(theta);
   return [gg_lng, gg_lat]
 }
 
 // gcj02坐标系转换为百度
 Utils.gcj02tobd09 = (lng, lat) => {
   let x_PI = 3.14159265358979324 * 3000.0 / 180.0;
   let z = Math.sqrt(lng * lng + lat * lat) + 0.00002 * Math.sin(lat * x_PI);
   let theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * x_PI);
   let bd_lng = z * Math.cos(theta) + 0.0065;
   let bd_lat = z * Math.sin(theta) + 0.006;
   return [bd_lng, bd_lat]
 }
 
 //手机号脱敏
 Utils.replaceNumber = (val) => {
   return val.replace(val.slice(3, 9), '******')
 }

 
 //对象转url参数格式
 Utils.queryParams = (data, isPrefix = false) => {
   let prefix = isPrefix ? '?' : ''
   let _result = []
   for (let key in data) {
     let value = data[key]
     if (['', undefined, null].includes(value)) {
       continue
     }
     if (value.constructor === Array) {
       value.forEach(_value => {
         _result.push(key + '[]=' + _value)
       })
     } else {
       _result.push(key + '=' + value)
     }
   }
   return _result.length ? prefix + _result.join('&') : ''
 }
 
 
 //获取url的code
 Utils.getUrlParam = (name) => {
 	var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
 	var r = window.location.search.substr(1).match(reg)
 	if (r != null) return unescape(r[2])
 	return null
 }
 
 
 //截取到最新code--url存在多个code时
 Utils.getParam = () => {
 	let url = window.location.href.split('&code=')
 	if (url.length > 1) {
 		let urls = url[url.length - 1]
 		let code = urls.split('&state=')[0]
 		return code ? code : null
 	} else {
 		let code = url[0].split('?code=')[1]
 		code = code.split('&state=')[0]
 		return code ? code : null
 	}
 }
 
 
 export default Utils
 